<script setup>
import { onMounted, ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService,artDelService } from '@/api/article'
import { formateTime } from '@/untils/format'
const artcileList = ref()
const total = ref(0)
const loading = ref(false)
const params = ref({
  pagenum: 1,
  pagesize: 10,
  cate_id: '',
  state: ''
})

onMounted(() => {
  getArticleList()
})

const onSearch = () => {
  getArticleList()
}

const onReset = () => {
  params.value = {
    pagenum: 1,
    pagesize: 10,
    cate_id: '',
    state: ''
  }
  getArticleList()
}

const getArticleList = async () => {
  loading.value = true
  await artGetListService(params.value).then((res) => {
    artcileList.value = res.data.data
    total.value = res.data.total
    loading.value = false
  })
}
const articleEditRef = ref()

const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onDelArticle = async(row) => {
  await ElMessageBox.confirm('是否确认删除该分类?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}
const onAddArticle = () => {
  articleEditRef.value.open({})
}
//添加或者编辑成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

const onSizeChange = (val) => {
  params.value.pagenum = 1
  params.value.pagesize = val
  getArticleList()
}
const handleCurrentChange = (val) => {
  params.value.pagenum = val
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id" width="240px"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state" style="width: 240px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="artcileList">
      <el-table-column align="center" label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="分类"
        prop="cate_name"
      ></el-table-column>
      <el-table-column header-align="center" align="center" label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formateTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="状态"
        prop="state"
      ></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template #default="{ row }">
          <el-button :icon="Edit" plain type="primary" @click="onEditArticle(row)">编辑</el-button>
          <el-button :icon="Delete" plain type="danger" @click="onDelArticle(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
  </page-container>
  <el-pagination
    v-model:current-page="params.pagenum"
    v-model:page-size="params.pagesize"
    :page-sizes="[1, 3, 5, 10]"
    :background="true"
    layout="total,sizes,prev,pager,next,jumper"
    :total="total"
    @size-change="onSizeChange"
    @current-change="handleCurrentChange"
    style="margin-top: 20px; justify-content: flex-end"
  >
  </el-pagination>
  <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
</template>
<style lang="scss" scoped></style>